home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / ptv1n4.arc / L1.ASM < prev    next >
Assembly Source File  |  1990-09-13  |  3KB  |  100 lines

  1.  
  2. ; *** Listing 1 ***
  3. ;
  4. ; Program to illustrate searching through a buffer of a specified
  5. ; length until either a specified byte or a zero byte is encountered.
  6. ; A standard loop terminated with LOOP is used.
  7.  
  8.     .model    small
  9.     .stack    100h
  10.     .data
  11. ; Sample string to search through.
  12. SampleString    label    byte
  13.     db    'This is a sample string of a long enough length '
  14.     db    'so that raw searching speed can outweigh any '
  15.     db    'extra set-up time that may be required.',0
  16. SAMPLE_STRING_LENGTH    equ    $-SampleString
  17.  
  18. ; User prompt.
  19. Prompt    db    'Enter character to search for:$'
  20.  
  21. ; Result status messages.
  22. ByteFoundMsg    db    0dh,0ah
  23.         db    'Specified byte found.',0dh,0ah,'$'
  24. ZeroByteFoundMsg db    0dh,0ah
  25.         db    'Zero byte encountered.',0dh,0ah,'$'
  26. NoByteFoundMsg    db    0dh,0ah
  27.         db    'Buffer exhausted with no match.',0dh,0ah,'$'
  28.  
  29.     .code
  30. Start    proc    near
  31.     mov    ax,@data    ;point to standard data segment
  32.     mov    ds,ax
  33.  
  34.     mov    dx,offset Prompt
  35.     mov    ah,9        ;DOS print string function
  36.     int    21h        ;prompt the user
  37.  
  38.     mov    ah,1        ;DOS get key function
  39.     int    21h        ;get the key to search for
  40.  
  41.     mov    ah,al        ;put character to search for in AH
  42.     mov    cx,SAMPLE_STRING_LENGTH ;# of bytes to search
  43.     mov    si,offset SampleString ;point to buffer to search
  44.     call    SearchMaxLength    ;search the buffer
  45.     mov    dx,offset ByteFoundMsg ;assume we found the byte
  46.     jc    PrintStatus    ;we did find the byte
  47.                 ;we didn't find the byte, figure out
  48.                 ; whether we found a zero byte or
  49.                 ; ran out of buffer
  50.     mov    dx,offset NoByteFoundMsg
  51.                 ;assume we didn't find a zero byte
  52.     jcxz    PrintStatus    ;we didn't find a zero byte
  53.     mov    dx,offset ZeroBytefoundMsg ;we found a zero byte
  54. PrintStatus:
  55.     mov    ah,9        ;DOS print string function
  56.     int    21h        ;report status
  57.  
  58.     mov    ah,4ch        ;return to DOS
  59.     int    21h
  60. Start    endp
  61.  
  62. ; Function to search a buffer of a specified length until either a
  63. ; specified byte or a zero byte is encountered.
  64. ;
  65. ; Input:
  66. ;    AH = character to search for
  67. ;    CX = maximum length to be searched (must be > 0)
  68. ;    DS:SI = pointer to buffer to be searched
  69. ;
  70. ; Output:
  71. ;    CX = 0 if and only if we ran out of bytes without finding
  72. ;        either the desired byte or a zero byte
  73. ;    DS:SI = pointer to searched-for byte if found, otherwise byte
  74. ;        after zero byte if found, otherwise byte after last
  75. ;        byte checked if neither searched-for byte nor zero
  76. ;        byte is found
  77. ;    Carry Flag = set if searched-for byte found, reset otherwise
  78.  
  79. SearchMaxLength    proc    near
  80.     cld
  81. SearchMaxLengthLoop:
  82.     lodsb            ;get the next byte
  83.     cmp    al,ah        ;is this the byte we want?
  84.     jz    ByteFound    ;yes, we're done with success
  85.     and    al,al        ;is this the terminating 0 byte?
  86.     jz    ByteNotFound    ;yes, we're done with failure
  87.     loop    SearchMaxLengthLoop ;it's neither, so check the next
  88.                 ; byte, if any
  89. ByteNotFound:
  90.     clc            ;return "not found" status
  91.     ret
  92. ByteFound:
  93.     dec    si        ;point back to the location at which
  94.                 ; we found the searched-for byte
  95.     stc            ;return "found" status
  96.     ret
  97. SearchMaxLength    endp
  98.     end    Start
  99.  
  100.